package org.htmlparser.lexer;

import java.io.IOException;
import org.htmlparser.util.ParserException;

public class StringSource extends Source
{
  protected String mEncoding;
  protected int mMark;
  protected int mOffset;
  protected String mString;

  public StringSource(String paramString)
  {
    this(paramString, "ISO-8859-1");
  }

  public StringSource(String paramString1, String paramString2)
  {
    if (paramString1 == null)
      str = "";
    while (true)
    {
      this.mString = str;
      this.mOffset = 0;
      this.mEncoding = paramString2;
      this.mMark = -1;
      return;
      str = paramString1;
    }
  }

  public int available()
  {
    int i;
    if (this.mString == null)
      i = 0;
    while (true)
    {
      return i;
      i = this.mString.length() - this.mOffset;
    }
  }

  public void close()
    throws IOException
  {
  }

  public void destroy()
    throws IOException
  {
    this.mString = null;
  }

  public char getCharacter(int paramInt)
    throws IOException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    if (paramInt >= this.mOffset)
      throw new IOException("read beyond current offset");
    return this.mString.charAt(paramInt);
  }

  public void getCharacters(StringBuffer paramStringBuffer, int paramInt1, int paramInt2)
    throws IOException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    if (paramInt1 + paramInt2 > this.mOffset)
      throw new IOException("read beyond end of string");
    paramStringBuffer.append(this.mString.substring(paramInt1, paramInt1 + paramInt2));
  }

  public void getCharacters(char[] paramArrayOfChar, int paramInt1, int paramInt2, int paramInt3)
    throws IOException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    if (paramInt3 > this.mOffset)
      throw new IOException("read beyond current offset");
    this.mString.getChars(paramInt2, paramInt3, paramArrayOfChar, paramInt1);
  }

  public String getEncoding()
  {
    return this.mEncoding;
  }

  public String getString(int paramInt1, int paramInt2)
    throws IOException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    if (paramInt1 + paramInt2 > this.mOffset)
      throw new IOException("read beyond end of string");
    return this.mString.substring(paramInt1, paramInt1 + paramInt2);
  }

  public void mark(int paramInt)
    throws IOException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    this.mMark = this.mOffset;
  }

  public boolean markSupported()
  {
    return true;
  }

  public int offset()
  {
    int i;
    if (this.mString == null)
      i = -1;
    while (true)
    {
      return i;
      i = this.mOffset;
    }
  }

  public int read()
    throws IOException
  {
    int i;
    if (this.mString == null)
      throw new IOException("source is closed");
    if (this.mOffset >= this.mString.length())
      i = -1;
    while (true)
    {
      return i;
      i = this.mString.charAt(this.mOffset);
      this.mOffset = (1 + this.mOffset);
    }
  }

  public int read(char[] paramArrayOfChar)
    throws IOException
  {
    return read(paramArrayOfChar, 0, paramArrayOfChar.length);
  }

  public int read(char[] paramArrayOfChar, int paramInt1, int paramInt2)
    throws IOException
  {
    int j;
    if (this.mString == null)
      throw new IOException("source is closed");
    int i = this.mString.length();
    if (this.mOffset >= i)
      j = -1;
    while (true)
    {
      return j;
      if (paramInt2 > i - this.mOffset)
        paramInt2 = i - this.mOffset;
      this.mString.getChars(this.mOffset, paramInt2 + this.mOffset, paramArrayOfChar, paramInt1);
      this.mOffset = (paramInt2 + this.mOffset);
      j = paramInt2;
    }
  }

  public boolean ready()
    throws IOException
  {
    int i;
    if (this.mString == null)
      throw new IOException("source is closed");
    if (this.mOffset < this.mString.length())
      i = 1;
    while (true)
    {
      return i;
      i = 0;
    }
  }

  public void reset()
    throws IllegalStateException
  {
    if (this.mString == null)
      throw new IllegalStateException("source is closed");
    if (-1 != this.mMark)
      this.mOffset = this.mMark;
    while (true)
    {
      return;
      this.mOffset = 0;
    }
  }

  public void setEncoding(String paramString)
    throws ParserException
  {
    this.mEncoding = paramString;
  }

  public long skip(long paramLong)
    throws IOException, IllegalArgumentException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    if (0L > paramLong)
      throw new IllegalArgumentException("cannot skip backwards");
    int i = this.mString.length();
    if (this.mOffset >= i)
      paramLong = 0L;
    while (true)
    {
      do
      {
        this.mOffset = (int)(paramLong + this.mOffset);
        return paramLong;
      }
      while (paramLong <= i - this.mOffset);
      paramLong = i - this.mOffset;
    }
  }

  public void unread()
    throws IOException
  {
    if (this.mString == null)
      throw new IOException("source is closed");
    if (this.mOffset <= 0)
      throw new IOException("can't unread no characters");
    this.mOffset -= 1;
  }
}